package com.edu.edumanager.service.imp;

import com.edu.edumanager.bean.EduChapter;
import com.edu.edumanager.bean.EduVideo;
import com.edu.edumanager.constant.EduConstant;
import com.edu.edumanager.exception.EduException;
import com.edu.edumanager.mapper.EduChapterMapper;
import com.edu.edumanager.mapper.EduVideoMapper;
import com.edu.edumanager.service.VideoService;
import com.edu.edumanager.utils.EduUploaderUtils;

import com.edu.edumanager.vo.EduResult;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.web.multipart.MultipartFile;
import ws.schild.jave.EncoderException;
import ws.schild.jave.MultimediaObject;
import ws.schild.jave.info.MultimediaInfo;

import java.io.File;
import java.io.IOException;

import java.util.Date;


/**
 * @program: edu-manager
 * @description:
 * @author: Jaime
 * @create: 2021-08-22 20:56
 **/
@Service
public class VideoServiceImpl implements VideoService {
    @Autowired
    EduVideoMapper videoMapper;

    @Autowired
    EduChapterMapper chapterMapper;





    @Override
    /**
     * 老师的实现方式
     */
    public EduResult uploadVideo(MultipartFile file) {
        //获取视频根路径
        File f = EduUploaderUtils.getRootFolder();
        if (!f.exists()) {
            f.mkdirs();
        }
        String name = file.getOriginalFilename();
        String videoName = EduUploaderUtils.getImageName(name);
        EduUploaderUtils.checkFilmFile(file);
        try {
            //上传视频成功
            file.transferTo(new File(f, videoName));
        } catch (IOException e) {
            e.printStackTrace();
            //走这里 代表 上面的try的代码有误 应该提示上传图片失败
            throw new EduException("上传视频失败");
        }
        //如果我们在这里吧一切算好了 发送到页面  但是用户又点击了取消 是不是白玩了 我在这里 只会给 页面想要的最低要求的数据给他 我绝对不会在这里来算
        return EduResult.ok().data("title", videoName).data("videoOriginalName", name).data("videoSourceId", EduConstant.HOST + EduUploaderUtils.getDateFolder() + "/" + videoName);
    }

    /**
     * 插入video数据
     *
     * @param video
     * @return
     */
    @Override
    public int addVideo(EduVideo video) {
        Date date = new Date();
        video.setGmtCreate(date);
        video.setGmtModified(date);
        video.setIsFree(true);
        File file = new File(EduConstant.IMAGEFILE,video.getVideoSourceId().substring(EduConstant.HOST.length()));

        video.setDuration(EduUploaderUtils.getVideoTime(file));
        video.setStatus("Normal");
        video.setVersion(1L);
        video.setSize(file.length());
        video.setGmtCreate(date);
        video.setGmtModified(date);
        System.out.println(video);
        int i = videoMapper.addVideo(video);
        if(i==0){
            throw new EduException("添加小节视频失败");
        }
        return i;
    }

    @Override
    public int deleteVideoByChapterId(Integer chapterId) {
        int i = videoMapper.deleteVideoByChapterId(chapterId);
        return i;
    }

    @Override
    public int deleteVideoById(Integer videoId) {
        int count = videoMapper.deleteVideoById(videoId);
        if(count == 0){
            throw new EduException("删除小节视频失败");
        }
        return count;
    }

    @Override
    public EduVideo getVideo(Integer videoId) {
        EduVideo video = videoMapper.getVideo(videoId);
        return video;
    }

    @Override
    public int updateVideo(EduVideo video) {
        Date date = new Date();
        File file = new File(EduConstant.IMAGEFILE,video.getVideoSourceId().substring(EduConstant.HOST.length()));
        MultimediaObject object = new MultimediaObject(file);
        try {
            MultimediaInfo info = object.getInfo();
            //视频时长
            video.setDuration(EduUploaderUtils.getVideoTime(file));
            video.setSize(file.length());
            video.setGmtModified(date);
            int i = videoMapper.updateVideo(video);
            if (i == 0) {
                throw new EduException("修改小节视频失败");
            }
            return i;
        } catch (EncoderException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int deleteVideoByCourseId(Integer courseId) {
        int i = videoMapper.deleteVideoByCourseId(courseId);
        return i;
    }





    /*   *//**
     * 按照list套list的方式构造数据 [{aaa,[{111,222}]},bbb,[{}]},]
     *
     * @param courseId
     * @return
     *//*
    @Override
    @Transactional(rollbackFor = EduException.class)
    public List<EduChapterList> getChapterList1(Integer courseId) {
        // 查询1级列表
        List<EduChapter> chaptersOne = chapterMapper.findChapter(courseId);
        List<EduChapterList> listResult = new ArrayList<EduChapterList>();
        for (EduChapter chapter : chaptersOne){
            EduChapterList chapterList1 =  new EduChapterList();
            chapterList1.setId(chapter.getId());
            chapterList1.setCourseId(chapter.getCourseId());
            chapterList1.setTitle(chapter.getTitle());
            chapterList1.setGmtCreated(chapter.getGmtCreated());
            chapterList1.setGmtModified(chapter.getGmtModified());
            chapterList1.setSort(chapter.getSort());
            // 循环里查二级列表
            List<EduVideo> videosOne = videoMapper.findVideo(courseId,chapter.getId());
            chapterList1.setChildren(videosOne);
            listResult.add(chapterList1);
        }
        return listResult;
    }*/

    //这是我自己写的返回video的方式
    /*@Override
    public EduResult uploadVideo(MultipartFile file) {
        //获取视频根路径
        File f = EduUploaderUtils.getRootFolder();
        if (!f.exists()) {
            f.mkdirs();
        }
        String fileName = UUID.randomUUID().toString() + file.getOriginalFilename();
        //校验格式
        EduUploaderUtils.checkFilmFile(file);
        try {
            file.transferTo(new File(f, fileName));
        } catch (IOException e) {
            e.printStackTrace();
            throw new EduException("上传视频失败");
        }
        String path = EduConstant.HOST+EduUploaderUtils.getDateFolder()+"/"+ fileName;
        EduVideo video = new EduVideo();
        // 大小B转换为kb
        long size = file.getSize();
        if (size < 1024) {
            size = 1;
        } else {
            size = size / 1024;
        }
        video.setSize(size);

        video.setTitle(file.getOriginalFilename());
        video.setVideoSourceId(path);
        video.setVideoOriginalName(file.getOriginalFilename());
        // 获取到视频时间
        String videoTime = EduUploaderUtils.getVideoTime(file);
        video.setDuration(videoTime);
        return EduResult.ok().data("video", video);
    }*/
}
